import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:getx_app/app/modules/home/models/user_info.dart';

class ProfileDetailController extends GetxController {
  final dio = Get.find<Dio>();

  UserInfo? userInfo;

  bool isLoading = false;

  final List<Map<String, dynamic>> genderList = const [
    {
      "title": "保密",
      "value": 0,
    },
    {
      "title": "男",
      "value": 1,
    },
    {
      "title": "女",
      "value": 2,
    }
  ];

  String findGenderByVal(int? val) {
    return genderList.firstWhereOrNull((i) => i["value"] == val)!["title"];
  }

  @override
  void onInit() {
    getUserInfo();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  Future<void> getUserInfo() async {
    isLoading = true;
    final res = await dio.get("/h5/learner/info");
    final data = UserInfo.fromJson(res.data["data"]);
    userInfo = data;
    isLoading = false;
    update();
  }

  void updateGender(int? val) {
    userInfo!.gender = val;
    update();
  }

  void updateBirthDate(DateTime val) {}

  @override
  void onClose() {}
}
